/**
 * 
 *  @project Air Traff Monitor
 * 
 *  @author M.Emre ATASEVER <m.emre.atas@gmail.com>
 *  @author M.Burak ALKAN <mburakalkan@gmail.com>
 *  @author H.Burak KARAMIK <bkaramik@gmail.com>
 *
 *	`Air Traffic Monitor' is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <QDockWidget>
#include <QAction>
#include <QIcon>
#include "kisock.h"

#include <QApplication>
#include <QDesktopWidget>
#include <QFrame>
#include <QPainter>
#include <QPen>
#include <QList>

#define HOR_SPC       2   /** Space between data points */
#define SCALE_WIDTH   75  /** Width of the scale */
#define MIN_SCALE     10  /** 10 kB/s is the minimum scale */  
#define SCROLL_STEP   4   /** Horizontal change on graph update */

#define BACK_COLOR    Qt::black
#define SCALE_COLOR   Qt::green
#define GRID_COLOR    Qt::darkGreen
#define RECV_COLOR    Qt::cyan
#define SEND_COLOR    Qt::yellow

#define FONT_SIZE     11

class QKismetGraph : public QFrame
{
	Q_OBJECT

	public:
		/** Bandwidth graph style. */
		enum GraphStyle {
		  SolidLine = 0,  /**< Plot bandwidth as solid lines. */
		  AreaGraph       /**< Plot bandwidth as alpha blended area graphs. */
		};

		QKismetGraph(QKismetSocket *s, QWidget *parent = 0);
		~QKismetGraph();

		void addPoints(qreal recv);
		/** Clears the graph. */
		void resetGraph();
		/** Toggles display of data counters. */
		void setShowCounters(bool showRecv);
		/** Sets the graph style used to display bandwidth data. */
		void setGraphStyle(GraphStyle style) { _graphStyle = style; }

	protected:
		/** Overloaded QWidget::paintEvent() */
		void paintEvent(QPaintEvent *event);

	private:
		/** Gets the width of the desktop, the max # of points. */
		int getNumPoints();

		/** Paints an integral and an outline of that integral for each data set
		 * (send and/or receive) that is to be displayed. */
		void paintData();
		/** Paints the send/receive totals. */
		void paintTotals();
		/** Paints the scale in the graph. */
		void paintScale();
		/** Returns a formatted string representation of total. */
		QString totalToStr(qreal total);
		/** Returns a list of points on the bandwidth graph based on the supplied set
		 * of send or receive values. */
		QVector<QPointF> pointsFromData(QList<qreal>* list);
		/** Paints a line with the data in <b>points</b>. */
		void paintLine(QVector<QPointF> points, QColor color, 
			       Qt::PenStyle lineStyle = Qt::SolidLine);
		/** Paints an integral using the supplied data. */
		void paintIntegral(QVector<QPointF> points, QColor color, qreal alpha = 1.0);

		/** Style with which the bandwidth data will be graphed. */
		GraphStyle _graphStyle;
		/** A QPainter object that handles drawing the various graph elements. */
		QPainter* _painter;
		/** Holds the received data points. */
		QList<qreal> *_recvData;
		/** Holds the sent data points. */
		//QList<qreal> *_sendData;
		/** The current dimensions of the graph. */
		QRect _rec;
		/** The maximum data value plotted. */
		qreal _maxValue;
		/** The maximum number of points to store. */
		int _maxPoints;
		/** The total data sent/recv. */
		//qreal _totalSend;
		qreal _totalRecv;
		/** Show the respective lines and counters. */
		bool _showRecv;
		//bool _showSend;

		int prev;


		QKismetSocket *sock;
	private slots:
		void addPoint(iInfo info);
};

/*class QKismetGraph : public QwtPlot
{
	Q_OBJECT

	public:
		QKismetGraph(QKismetSocket *s);
	private:

		int prev,size;
		QPen *pen;
	private slots:
		void addPoint(iInfo info);
};*/

class QKismetGraphDock : public QDockWidget
{
	Q_OBJECT

	public:
		QKismetGraphDock(QKismetSocket *s);
	private:
		QKismetGraph *graph;
};

#ifndef  GRAPH_INC
#define  GRAPH_INC

#endif
